package com.freelancer.musicinstruction.activity.delegates;

import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Callable;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.freelancer.musicinstruction.R;
import com.freelancer.musicinstruction.beans.interfaces.MusicInstruction;
import com.freelancer.musicinstruction.beans.youtube.YoutubeVideo;


public class YoutubeVideoAdapter  extends ArrayAdapter<MusicInstruction> {
		
		private static final String TAG = YoutubeVideoAdapter.class.getSimpleName();
	
        private List<MusicInstruction> videos;
        private Context context;
        
        Handler handler;
        
        LayoutInflater inflater;
        YoutubeVideo video;
        
        Bitmap bitmap;
        Canvas canvas;
        Paint paint;
        
        

        public YoutubeVideoAdapter(Context context, int resource, List<MusicInstruction> videos) {
                super(context, resource, videos);
                this.videos = videos;
                this.context = context;
                this.handler = new Handler();
                this.inflater = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
               /* this.paint = new Paint();
				paint.setAntiAlias( true );
				paint.setColor( Color.WHITE );
				paint.setTextSize( 12.0f );
				paint.setTextAlign( Align.LEFT);*/
        }
        
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        		
        		final ViewHolder viewHolder;
        		
                
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.youtube_listitem, null);
                    
                    viewHolder = new ViewHolder();
                    viewHolder.titleText = ((TextView) convertView.findViewById(R.id.titletext));
                    viewHolder.dateText = ((TextView) convertView.findViewById(R.id.datetext));
                    viewHolder.summaryText = ((TextView) convertView.findViewById(R.id.summarytext));
                    viewHolder.ratingBar = ((RatingBar)convertView.findViewById( R.id.ratingbar ));
                    viewHolder.thumb_box = ((ImageView)convertView.findViewById( R.id.thumbnail_box));
                    
                    convertView.setTag( viewHolder );
                }
                else{
                	viewHolder = (ViewHolder)convertView.getTag( );
                }
                
                video = (YoutubeVideo) videos.get( position );
                
                if (video != null) {
                	
                	viewHolder.titleText.setText( video.getVideoTitle() );
                	viewHolder.titleText.setTypeface( Typeface.DEFAULT_BOLD );
                	
                	viewHolder.dateText.setText( video.getPublishDate() + " by " + video.getVideoUploader());
                	viewHolder.dateText.setTypeface( Typeface.MONOSPACE );
                	
                	float ratingValue;
                	
                	//Log.d(TAG, "video rating = "+video.getVideoRating());
                	if( video.getVideoRating() == null ){
                		ratingValue = 0;
                	}
                	else{
                		ratingValue = Float.valueOf( video.getVideoRating()).floatValue();
                	}
                	viewHolder.ratingBar.setRating( ratingValue );
                	
                	viewHolder.summaryText.setText( video.getVideoDescription() );
                	
                	viewHolder.thumb_box.setImageResource( R.drawable.ic_video_empty );
                	
                
                	
                	try {
                		//Log.d(TAG, "HEIGHT= "+viewHolder.thumb_box.getHeight());
                		//Log.d(TAG, "WIDTH= "+viewHolder.thumb_box.getWidth());
                		
                		
						
						viewHolder.thumb_box.setImageBitmap
							( Bitmap.createScaledBitmap( (Bitmap)(new Callable(){

								@Override
								public Object call() throws Exception {
									Log.d(TAG, "getting bitmap over http");
									URL url = new URL( video.getThumbnailURL() );
									return BitmapFactory.decodeStream ( url.openStream() );
									//imageStream = url.openStream();
									//canvas = new Canvas( bitmap );
			                		//canvas.drawText( viewHolder.getDurationString( video.getVideoLength()), 50, 50, paint);
									
									//return bitmap;
								}
								
							}).call(), 100, 70, true ));
					} catch (Exception e) {
						
						Log.d(TAG, e.getMessage() );
					}
 	
                }
                
                return convertView;
        }
        
        
        static class ViewHolder{
        	
    		

			ImageView thumb_box;
            TextView titleText;
            TextView dateText;
            TextView summaryText;
            RatingBar ratingBar;
            
            
            public ImageView getThumb_box() {
				return thumb_box;
			}

			public void setThumb_box(ImageView thumbBox) {
				thumb_box = thumbBox;
			}

			public TextView getTitleText() {
				return titleText;
			}

			public void setTitleText(TextView titleText) {
				this.titleText = titleText;
			}

			public TextView getDateText() {
				return dateText;
			}

			public void setDateText(TextView dateText) {
				this.dateText = dateText;
			}

			public TextView getSummaryText() {
				return summaryText;
			}

			public void setSummaryText(TextView summaryText) {
				this.summaryText = summaryText;
			}

			public RatingBar getRatingBar() {
				return ratingBar;
			}

			public void setRatingBar(RatingBar ratingBar) {
				this.ratingBar = ratingBar;
			}
			
            public String getDurationString( String durationInSeconds ){
            	
            	StringBuilder sb = new StringBuilder();
            	sb.append( "(");
            	sb.append( Integer.parseInt( durationInSeconds ) / 60 );
            	sb.append( ":");
            	sb.append( Integer.parseInt( durationInSeconds ) % 60 );
            	sb.append( ")");
            	
            	return sb.toString();
            }
 
    	}
 	
}


	interface onBitmapReadyListener{
		
		public abstract void onBitmapReady( Bitmap bitmap );
		
	}


	
